package com.kvest.mobile_newspaper.activities;

import android.util.DisplayMetrics;
import android.util.Log;
import com.kvest.mobile_newspaper.R;
import com.kvest.mobile_newspaper.core.ArticlesManager;
import com.kvest.mobile_newspaper.core.Settings;
import com.kvest.mobile_newspaper.datamodel.Article;
import com.kvest.mobile_newspaper.datastorage.ImageStorage;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class BaseArticleActivity extends NewspaperBaseActivity
{
	public static final String ARTICLE_ID = "article_id";
	protected String articleId;
	protected boolean articleExists;
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.article);
        
        //�������� �� ������
        if (getIntent().getExtras().containsKey(ARTICLE_ID))
        {
        	articleId = getIntent().getExtras().getString(ARTICLE_ID);
        }
        else
        {
        	articleId = "";
        }

        //������������� ���� �������
        setContent();
	}
	
	private void setContent()
	{
		float textSize = Settings.FONT_SIZE_OFFSET + Settings.getSettings().getFontSize();
		int fontColor = Settings.getSettings().getFontColor();
		int fontBgColor = Settings.getSettings().getFontBgColor();
		
		findViewById(R.id.article_main_scroll).setBackgroundColor(fontBgColor);
		
		//�������� ������
		Article article = ArticlesManager.getManager().getArticle(articleId);
		
		//���� �� ����� ������
		if (article == null)
		{
			articleExists = false;
			TextView textView = (TextView)findViewById(R.id.article_name);
			textView.setText(R.string.no_article_data);
			textView.setTextSize(textSize);
			textView.setTextColor(fontColor);
			textView.setBackgroundColor(fontBgColor);
			return;
		}
		
		articleExists = true;
		setTitle(article.getName());

		//��������� ������
		TextView textView = (TextView)findViewById(R.id.article_name);
		textView.setText(article.getName());
		textView.setTextSize(textSize);
		textView.setTextColor(fontColor);
		textView.setBackgroundColor(fontBgColor);
		
		//��������
		if (article.getDetailImage() != null)
		{
			((ImageView)findViewById(R.id.article_picture)).setImageBitmap(article.getDetailImage());
		}
		else
		{
			findViewById(R.id.article_picture).setVisibility(View.INVISIBLE);
		}
		
		//����
		textView = (TextView)findViewById(R.id.article_date);
		textView.setText(article.getDate());
		textView.setTextSize(textSize);
		textView.setTextColor(fontColor);
		textView.setBackgroundColor(fontBgColor);
		
		ImageGetter getter = new ImageGetter()
		{	
			public Drawable getDrawable(String source) 
			{
				Drawable result = new BitmapDrawable(getResources(), ImageStorage.loadImageForArticle(articleId, source));



                DisplayMetrics metrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(metrics);

                int newWidth = metrics.widthPixels - Math.round(getResources().getDimension(R.dimen.scroll_size)) -
                                                     2 * Math.round(getResources().getDimension(R.dimen.main_menu_item_padding));
                if (result.getIntrinsicWidth() > newWidth) {
                    //Пропорционально считаем высоту
                    int height = (int)((float)result.getIntrinsicHeight() *
                                        ((float)newWidth / (float)result.getIntrinsicWidth()));
                    result.setBounds(0, 0, newWidth, height);
                } else {
                    result.setBounds(0, 0, result.getIntrinsicWidth(), result.getIntrinsicHeight());
                }
				return result;
			}
		};
		
		//����� ������
		textView = (TextView)findViewById(R.id.article_text);
		textView.setText(Html.fromHtml(article.getDetailText(), getter, null));
		textView.setTextSize(textSize);
		textView.setTextColor(fontColor);
		textView.setBackgroundColor(fontBgColor);
		
		ArticlesManager.getManager().setReadArticle(article.getId());
	}
}
